package com.mida.app.knw.controller;

import com.mida.app.knw.service.KnwCommonWordsTreeService;
import com.mida.dms.knw.entity.KnwCommonWordsTree;
import com.mida.dms.knw.vo.CommonWordsTreeDto;
import com.mida.dms.knw.vo.CommonWordsTreeVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.mida.tool.common.result.BaseResult.buildActionWarn;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

@RestController
@RequestMapping("/knwCommonWordsTree")
@Api(tags = "常用语模板")
@Slf4j
@CrossOrigin
public class KnwCommonWordsTreeController {
    @Autowired
    private KnwCommonWordsTreeService knwCommonWordsTreeService;
    /**
     * 1、新增常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/addCommonWordsTree",consumes = {"application/json"})
    public ActionResult addCommonWordsTree(@RequestBody CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult result = new ActionResult();
        try {
            return knwCommonWordsTreeService.addCommonWordsTree(commonWordsTreeVo);
        } catch (Exception e) {
            log.error("新增常用语失败", e);
            buildActionWarn(result, "新增常用语失败");
        }
        return result;
    }

    /**
     * 2、删除常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/deleteCommonWordsTree",consumes = {"application/json"})
    public ActionResult deleteCommonWordsTree(@RequestBody CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult result = new ActionResult();
        try {
            return knwCommonWordsTreeService.deleteCommonWordsTree(commonWordsTreeVo);
        } catch (Exception e) {
            log.error("删除常用语失败", e);
            buildActionWarn(result, "删除常用语失败");
        }
        return result;
    }
    /**
     * 3、修改常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/updateCommonWordsTree",consumes = {"application/json"})
    public ActionResult updateCommonWordsTree(@RequestBody CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult result = new ActionResult();
        try {
            return knwCommonWordsTreeService.updateCommonWordsTree(commonWordsTreeVo);
        } catch (Exception e) {
            log.error("修改常用语失败", e);
            buildActionWarn(result, "修改常用语失败");
        }
        return result;
    }


    /**
     * 4、查询多层树形常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/selectCommonWordsTrees")
    public ListResult<List<CommonWordsTreeVo>> selectCommonWordsTrees(@RequestBody CommonWordsTreeVo commonWordsTreeVo) {
        ListResult listResult = new ListResult();
        try {
            return knwCommonWordsTreeService.selectCommonWordsTrees(commonWordsTreeVo);
        } catch (Exception e) {
            log.error("查询多层树形常用语失败", e);
            buildListWarn(listResult, "查询多层树形常用语失败");
            return listResult;
        }
    }


    /**
     * 5、排序常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/sortCommonWordsTree",consumes = {"application/json"})
    public ActionResult sortCommonWordsTree(@RequestBody List<KnwCommonWordsTree> commonWordsTreeSoids) {
        ActionResult result = new ActionResult();
        try {
            return knwCommonWordsTreeService.sortCommonWordsTree(commonWordsTreeSoids);
        } catch (Exception e) {
            log.error("排序常用语失败", e);
            buildActionWarn(result, "排序常用语失败");
        }
        return result;
    }


    /**
     * 7、分享常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/shareCommonWordsTrees",consumes = {"application/json"})
    public ActionResult shareCommonWordsTrees(@RequestBody CommonWordsTreeVo commonWordsTreeVo){
        ActionResult result = new ActionResult();
        try {
            return knwCommonWordsTreeService.shareCommonWordsTrees(commonWordsTreeVo);
        } catch (Exception e) {
            log.error("分享常用语失败", e);
            buildActionWarn(result, "分享常用语失败");
        }
        return result;
    }


    /**
     * 7.1、删除时查询节点下是否有常用语
     *
     * @param
     * @return
     */
    @PostMapping(value = "/isDelete",consumes = {"application/json"})
    public Integer isDelete(@RequestBody CommonWordsTreeVo commonWordsTreeVo){
        return knwCommonWordsTreeService.isDelete(commonWordsTreeVo);
    }

}
